import {
	showToast
} from 'vant';
const dateFormat = (t) => {
	const date = new Date(t),
		months = date.getMonth() + 1,
		days = zeroFixed(date.getDate()),
		hours = zeroFixed(date.getHours()),
		minutes = zeroFixed(date.getMinutes()),
		weeks = weeksFormat(date.getDay());

	return `${months}月${days}日（${weeks}）${hours}:${minutes}`
}

const zeroFixed = (t) => {
	return t < 10 ? '0' + t : t;
}

const weeksFormat = (t) => {
	const word = ['日', '一', '二', '三', '四', '五', '六'];
	return '周' + word[t];
}

// 作业时间状态
function getWorkStatus(deadline) {
	const currentTime = new Date().getTime();
	const endTime = new Date(deadline).getTime();

	let timeDiff = endTime - currentTime;

	if (timeDiff <= 0) {
		return '已结束';
	}

	// 计算剩余的天数、小时、分钟和秒
	const days = Math.floor(timeDiff / (1000 * 60 * 60 * 24));
	timeDiff -= days * (1000 * 60 * 60 * 24);

	const hours = Math.floor(timeDiff / (1000 * 60 * 60));
	timeDiff -= hours * (1000 * 60 * 60);

	const minutes = Math.floor(timeDiff / (1000 * 60));
	timeDiff -= minutes * (1000 * 60);

	return `进行中-${days}天${hours}小时${minutes}分钟后截止`;
}
// 验证是否录音授权  
function validateAuthAudio() {
	return new Promise((resolve, reject) => {
		navigator.mediaDevices.getUserMedia({
			audio: true
		}).then(function(stream) {
			// 用户已经授权录音  
			resolve(true);
			// 可以在这里关闭流，如果不再需要  
			stream.getTracks().forEach(track => track.stop());  
		}).catch(function(err) {
			// 用户拒绝授权或发生其他错误  
			reject(false);
			if (err.name === "NotFoundError" || err.name === "DevicesNotFoundError") {
				showToast('没有找到录音设备');
			} else if (err.name === "PermissionDeniedError" || err.name === "SecurityError") {
				showToast('用户拒绝授权录音');
			} else if (err.name === "AbortError") {
				showToast('用户取消了录音授权请求');
			} else {
				showToast('发生其他错误');
			}
		});
	});
}

export {
	dateFormat,
	getWorkStatus,
	validateAuthAudio,
};